package com.example.myschool;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.myschool.fragment.HomeFragment;
import com.example.myschool.fragment.MineFragment;
import com.example.myschool.fragment.NewsFragment;
import com.example.myschool.fragment.ServicesFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView mBottomNavigationView;

    private HomeFragment mHomeFragment;
    private ServicesFragment mServicesFragment;
    private NewsFragment mNewsFragment;
    private MineFragment mMineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBottomNavigationView = findViewById(R.id.bottomNavigationView);
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                if (menuItem.getItemId()==R.id.home){
                    selectedFragment(0);

                }else if (menuItem.getItemId()==R.id.services){
                    selectedFragment(1);

                } else if (menuItem.getItemId()==R.id.news) {
                    selectedFragment(2);

                }else {
                    selectedFragment(3);

                }
                return true;
            }
        });


     selectedFragment(0);
    }

    private void selectedFragment(int position) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        if (position==0){
            if (mHomeFragment ==null){
                mHomeFragment = new HomeFragment();
                fragmentTransaction.add(R.id.content,mHomeFragment);
            }else {
                fragmentTransaction.show(mHomeFragment);
            }
        } else if (position==1) {
            if (mServicesFragment ==null){
                mServicesFragment = new ServicesFragment();
                fragmentTransaction.add(R.id.content,mServicesFragment);
            }else {
                fragmentTransaction.show(mServicesFragment);
                mServicesFragment.loadData();
            }

        } else if (position==2) {
            if (mNewsFragment ==null){
                mNewsFragment = new NewsFragment();
                fragmentTransaction.add(R.id.content,mNewsFragment);
            }else {
                fragmentTransaction.show(mNewsFragment);
                 mNewsFragment.loadData();
            }
        }else {
            if (mMineFragment ==null){
                mMineFragment = new MineFragment();
                fragmentTransaction.add(R.id.content,mMineFragment);
            }else {
                fragmentTransaction.show(mMineFragment);
            }
        }
        fragmentTransaction.commit();


    }


        private void hideFragment(FragmentTransaction fragmentTransaction){
        if (mHomeFragment != null) {
            fragmentTransaction.hide(mHomeFragment);
        }
        if (mServicesFragment != null) {
            fragmentTransaction.hide(mServicesFragment);
        }
        if (mNewsFragment != null) {
            fragmentTransaction.hide(mNewsFragment);
        }

        if (mMineFragment != null) {
            fragmentTransaction.hide(mMineFragment);
        }

    }
}